<?php

/**
 * 台湾CMS系统 twCMS
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2024.10.13
 * Update at: 2025.05.04
*/

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Services\User\Auth as AuthService;
use App\Services\User\ValidatorUser;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Validation\ValidationException;

class AuthController extends Controller
{
    /**
     * 用户登陆窗体
     *
     * @param AuthService $auth
     * @param  string  $accountType
     * @return View|RedirectResponse
     */
    public function index(AuthService $auth, string $accountType): View|RedirectResponse
    {
        if($auth->isLoggedIn()){
            return redirect(route('auth.dashboard', ['accountType' => $accountType]))->withSuccess('您已成功登陆！');
        }

        return view('auth.login', ['title' => '用户登陆', 'accountType' => $accountType]);
    }

    /**
     * 注册窗体
     *
     * @param AuthService $auth
     * @param  string  $accountType
     * @return View|RedirectResponse
     */
    public function registration(AuthService $auth, string $accountType): View|RedirectResponse
    {
        if($auth->isLoggedIn()){
            return redirect(route('login', ['accountType' => $accountType]))->withSuccess('您已成功登陆！');
        }

        return view('auth.registration', ['title' => '用户注册', 'accountType' => $accountType]);
    }

    /**
     * 登陆
     *
     * @param Request $request
     * @param ValidatorUser $validator
     * @param AuthService $auth
     * @param  string  $accountType
     * @return RedirectResponse
     */
    public function postLogin(Request $request, ValidatorUser $validator, AuthService $auth, string $accountType): RedirectResponse
    {
        try {
            // 数据验证
            $validated = $validator->login($request->all());
        }
        catch (ValidationException $e) {

            return back()->withErrors($e->errors())->withInput();
        }

        if ($auth->login($validated)) {

            return redirect()->intended(route('auth.dashboard', ['accountType' => $accountType]))
                ->withSuccess('您已成功登陆！');
        }

        return redirect(route('login', ['accountType' => $accountType]))->withError('Oppes! You have entered invalid credentials');
    }

    /**
     * 会员注册
     *
     * @param Request $request
     * @param ValidatorUser $validator
     * @param AuthService $auth
     * @param  string  $accountType
     * @return RedirectResponse
     */
    public function postRegistration(Request $request, ValidatorUser $validator, AuthService $auth, string $accountType): RedirectResponse
    {
        try {
            // 数据验证
            $validated = $validator->register($request->all());
        }
        catch (ValidationException $e) {

            return back()->withErrors($e->errors())->withInput();
        }

        $auth->register($validated);

        return redirect(route('auth.dashboard', ['accountType' => $accountType]))->withSuccess('您已成功注册会员！');
    }

    /**
     * 登出
     *
     * @param  AuthService  $auth
     * @param  string  $accountType
     * @return  RedirectResponse
     */
    public function logout(AuthService $auth, string $accountType): RedirectResponse
    {
        $auth->logout();

        return Redirect(route('login', ['accountType' => $accountType]));
    }
}
